﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;

namespace CommunitySystem.Views
{
    public partial class ReturnAsyncCtrl : UserControl, IAsyncCtrl
    {
        private UIElement _child;

        public NavMoveSwitch Nav { get; set; }

        public IReturnListenerc PrevPage { get; set; }

        public bool CanReturn
        {
            get { return stackReturn.Visibility == Visibility.Visible; }
            set { stackReturn.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }

        public string Title
        {
            get { return TitleTB.Text = Title; }
            set { TitleTB.Text = value; }
          
        }

        //public UIElement TitleContent
        //{
        //    get { return titleBar.Content; }
        //    set { titleBar.Content = value; }
        //}

        public ReturnAsyncCtrl(UIElement child)
        {
            InitializeComponent();

           // LayoutRoot.Children.Remove(stackReturn);
            //titleBar.Content = stackReturn;

            _child = child;
            //LayoutRoot.Children.Remove(child);
            
            childHolder.Children.Add(child as UIElement);
        }

        public bool PageLoaded
        {
            get
            {
                IAsyncCtrl asyncCtrl = _child as IAsyncCtrl;
                if (asyncCtrl != null)
                {
                    return asyncCtrl.PageLoaded;
                }
                return true;
            }
            set
            {
                IAsyncCtrl asyncCtrl = _child as IAsyncCtrl;
                if (asyncCtrl != null)
                {
                    asyncCtrl.PageLoaded = value;
                }
            }
        }

        private void returnPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Nav.MovePrev();
            if (PrevPage != null)
            {
                PrevPage.BeginReturn();
            }

            returnPanel.MouseLeftButtonDown -= returnPanel_MouseLeftButtonDown;
        }
    }
}
